package cz.jm.web.jmlib.client.dialog;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

import cz.jm.web.jmlib.client.dialog.items.DialogItem;
import cz.jm.web.jmlib.client.dialog.validation.MultipleValidable;

/** DialogContentIconWidget je jednoducha implementacia rohrania DialogContent, ktora je 
 * vsak vo vacsine pripadou postacujuca.
 * Do widgetu je mozne vkladat polozky ({@linkplain #addItem(DialogItem)}), ktore sa 
 * vkladaju vertikalne pod seba. Obsah naviac zobrazuje ikonu, ktora sa 
 * zobrazuje vlavo od samotnych vkladanych prvkov dialogu. Naviac je poskytnuta moznost 
 * skupinovej validacie vsetkych vlozenych ovladacich prvkov do dialogu.
 * */
public class DialogContentIconWidget extends Composite implements DialogContent {
	/** Skupinova validacia. */
	private final MultipleValidable validation = new MultipleValidable();
	/** Panel do ktoreho sa ukladaju zlozky dialogu (odlozene vkladanie prvkov dialogu). */
	private final VerticalPanel dialogItemsHolder = new VerticalPanel();
	/** Panel ktory drzi ikonu (odolozene nastavenie ikony).*/
	private FlowPanel iconHolder = new FlowPanel();
		
	
	public DialogContentIconWidget() {		
		DecoratorPanel mainPanel = new DecoratorPanel();
		initWidget(mainPanel);
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.add(iconHolder);		
		horizontalPanel.add(dialogItemsHolder);
		mainPanel.add(horizontalPanel);
		// Pokial je obsah dialogu mensi ako ikona tak posobi lepsie zarovnanie obsahu na stred.
		horizontalPanel.setCellVerticalAlignment(dialogItemsHolder, HasVerticalAlignment.ALIGN_MIDDLE);
		
//		mainPanel.getElement().getStyle().setBackgroundColor("red");
//		verticalPanel.getElement().getStyle().setBackgroundColor("blue");
//		horizontalPanel.getElement().getStyle().setBackgroundColor("green");
//		iconHolder.getElement().getStyle().setBackgroundColor("orange");
    }
	
	public DialogContentIconWidget(Image icon) {
		this();
		setIcon(icon);
	}
	
	@Override
	public void setIcon(Image icon) {
		iconHolder.clear();
		iconHolder.add(icon);
	}
	
	@Override
	public void addItem(DialogItem item) {
		dialogItemsHolder.add(item);
		validation.addValidatedItem(item);
	}

	@Override
    public boolean isValid() {
	    return validation.isValid();
    }
	
}
